using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace FrameWork
{
    public class FsmModule : BaseModule
    {
        private Dictionary<int, FsmBase> _fsmDic;

        private int _serializeId;

        public override void Init()
        {
            _fsmDic = new Dictionary<int, FsmBase>();
        }

        public override void ShutDown()
        {
            foreach(FsmBase fsmBase in _fsmDic.Values)
            {
                fsmBase.ShowDown();
            }

            _fsmDic.Clear();
        }

        public override void Update(float deltaTime, float realDeltaTime)
        {
            foreach(FsmBase fsmBase in _fsmDic.Values)
            {
                fsmBase.Update(deltaTime);
            }
        }

        /// <summary>
        /// 需要处理返回创建问题，用Int还是string作为id
        /// </summary>
        public Fsm<T> Create<T>(T owner,List<FsmState<T>> states) where T:class
        {
            int id = _serializeId++;
            if (_serializeId == int.MaxValue)
                _serializeId = 0;
            Fsm<T> fsm = Fsm<T>.Create(id, owner, states);
            _fsmDic.Add(id, fsm);
            return fsm;
        }

        /// <summary>
        /// 获取状态机
        /// </summary>
        /// <param name="id">状态机id</param>
        public FsmBase GetFsm(int id)
        {
            if(!_fsmDic.TryGetValue(id,out FsmBase fsm))
            {
                Debug.LogError($"不包含该fsm,请检查id{id}");
            }

            return fsm;
        }
    }
}

